package com.wolfheader.wolfgame.service.admin.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wolfheader.wolfgame.bean.admin.GameBean;
import com.wolfheader.wolfgame.dao.admin.GameDao;
import com.wolfheader.wolfgame.entity.admin.GameEntity;
import com.wolfheader.wolfgame.service.admin.AdminService;
import com.wolfheader.wolfgame.service.admin.GameService;
import com.wolfheader.wolfgame.service.impl.BaseServiceImpl;

@Service("gameServiceImpl")
public class GameServiceImpl extends BaseServiceImpl<GameEntity> implements GameService {

	@Autowired
	private GameDao gameDao;

	@Autowired
	private AdminService adminService;

	@Override
	public GameEntity add(GameBean bean) {
		// TODO Auto-generated method stub
		GameEntity game = new GameEntity();
		game.setAdminName(adminService.getCurrentUsername());
		game.setDateRank(bean.isDateRank());
		game.setClearDate(bean.getClearDate());
		game.setEndDate(bean.getEndDate());
		game.setMaxAssCount(bean.getMaxAssCount());
		game.setMaxScore(bean.getMaxScore());
		game.setName(bean.getName());
		game.setRemark(bean.getRemark());
		game.setSearchKey(bean.getSearchKey());
		game.setStartDate(bean.getStartDate());
		game.setGamePath(bean.getGamePath());
		if (!bean.isWeixinFlag()) {
			game.setWeiXinAccount(bean.getWeiXinAccount());
			game.setWeixinFlag(bean.isWeixinFlag());
			game.setWeiXinPassword(bean.getGamePath());
		}
		return gameDao.save(game);
	}

	@Override
	public Object checkGameOutTime(GameEntity game, String returnType) {
		Map<String, Object> map = new HashMap<String, Object>();

		Long dateTime = new Date().getTime();
		boolean hasStartDate = game.getStartDate() != null;
		boolean hasEndDate = game.getEndDate() != null;
		Long startDateTime = game.getStartDate().getTime();
		Long endDateTime = game.getEndDate().getTime();

		// 开始时间没有设置，设置了结束时间
		if (!hasStartDate && hasEndDate) {
			// 超过了结束时间
			if (dateTime > endDateTime) {
				map.put("status", "0");
				map.put("msg", "游戏活动已经结束");
			} else {
				map.put("status", "1");
			}
			// 设置了开始时间和结束时间
		} else if (hasStartDate && hasEndDate) {
			if (dateTime > startDateTime && dateTime < endDateTime) {
				map.put("status", "1");
			} else {
				map.put("status", "0");
				map.put("msg", "不在游戏活动时间");
			}
			// 未设置开始和结束时间
		} else if (!hasStartDate && !hasEndDate) {
			map.put("status", "1");
		} else if (hasStartDate && !hasEndDate) {
			if (dateTime > startDateTime) {
				map.put("status", "1");
			} else {
				map.put("status", "0");
				map.put("msg", "不在游戏活动时间");
			}
		}
		// 0 为返回map 1 为返回boolean
		if (returnType.equals("1")) {
			String status = (String) map.get("status");
			return status.equals("1");
		}
		// TODO Auto-generated method stub
		return map;
	}

}
